package com.richpencil.modules.weixin.rest;

import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.richpencil.modules.system.rest.BaseController;
import com.richpencil.modules.weixin.domain.SysEnum;
import com.richpencil.modules.weixin.domain.UnifiedOrderObject;
import com.richpencil.modules.weixin.domain.WaswxmpConfig;
import com.richpencil.modules.weixin.service.WechatHelper;
import com.richpencil.modules.weixin.service.WxService;
import com.richpencil.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("hos/weixin")
public class WxControler extends BaseController {
    @Autowired
    com.richpencil.config.ProjectConfigInfo ProjectConfigInfo;
    @Autowired
    WxService wxService;
    public boolean checkistest(HttpServletRequest request)
    {
        if(ProjectConfigInfo.isdebugmode()==true) {
            return true;
        } else {
            return false;
        }
    }
    @RequestMapping("weixinenter")
    public String weixinenter(HttpServletRequest request) throws Exception {
        String fid=this.getParameter(request,"fid");
        String fsysid=this.getParameter(request,"fsysid");
        String fconfigid=this.wxService.getDefaultOne().fconfigid;
        WaswxmpConfig config=this.wxService.getDefaultOne();
        String WeixinAuthUrl=this.wxService.encodeWeixinAuthUrl(fconfigid,fsysid,fid);
        WeixinAuthUrl = "redirect:"+WeixinAuthUrl;
        return WeixinAuthUrl;
    }

    @RequestMapping("weixinenterwas")
    public String weixinenterwas(HttpServletRequest request) throws Exception {

        String fid="";
        String fsysid= SysEnum.Was.fsysid.toString();
        String fconfigid=this.wxService.getDefaultOne().fconfigid;
        WaswxmpConfig config=this.wxService.getDefaultOne();
        String WeixinAuthUrl=this.wxService.encodeWeixinAuthUrl(fconfigid,fsysid,fid);
        WeixinAuthUrl = "redirect:"+WeixinAuthUrl;
        return WeixinAuthUrl;
    }


    @RequestMapping("weixinauth")
    private String weixinauth(HttpServletRequest request,String fconfigid ,Integer fsysid,String fid) throws Exception {
        String appId =this.wxService.getDefaultOne().fappid;
        String RedirectUrl ="";

            RedirectUrl=this.ProjectConfigInfo.authorizeurl;

        String fconfigidString=StringHelper.resetValue(fconfigid);
        String fsysidString=StringHelper.resetValue(fsysid);
        String fidString=StringHelper.resetValue(fid);
        String state=this.wxService.encodeState(fconfigidString, fsysidString,fidString);
        String WeixinUrl = "redirect:" + SnsAccessTokenApi.getAuthorizeURL(appId, RedirectUrl,state, false);
        return WeixinUrl;
    }

    // 保存用户付款结果
    @RequestMapping("weixinnotifyurl")
    private void weixinnotifyurl(HttpServletResponse response, HttpServletRequest request) throws Exception {
        UnifiedOrderObject uobject = WechatHelper.getRequestReturnCode(request);
        if (uobject.isSuccess()) {
            //RedPackObject result = voteApiService.updateBillStateAndSendRedPack(uobject.outtradeno);
            //loginfo(result.message);
        }
    }
}
